package org.fluidripple.socket.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.fluidripple.file.DeviceDataMappedFile;
import org.fluidripple.socket.session.Session;
import org.fluidripple.socket.wrapper.DataWrapper;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class DeviceDataCollectProtocol extends Protocol{


    private Map<String,DeviceDataMappedFile> map = new HashMap<>();

    @Override
    public void writer(Channel channel) {

    }

    @Override
    public void toDecode(DataWrapper dataWrapper) {
        ByteBuf buf = dataWrapper.getBuf();
        Session session = dataWrapper.getSession();
        session.setCollectData(true);
        String deviceSerializable = dataWrapper.getDeviceSerializable();
        //因为他的设备量少，所以这里就是一个设备启动一个线程
        //如果设备量较多，这里就需要优化，使用线程池的方式
        DeviceDataMappedFile deviceDataMappedFile = map.computeIfAbsent(deviceSerializable, k -> new DeviceDataMappedFile(k));
        deviceDataMappedFile.appendMessage(buf);

    }
}
